using UnityEngine;

public class PlayerNick : MonoBehaviour
{
	public HealthBar3D healthBar;

	public Renderer pointer;

	public Renderer leaderCrown;

	public TextMesh textField;

	public MeshRenderer textRender;

	public Material teamMaterial;

	public Material enemyMaterial;

	public Color colorLeader = Color.yellow;

	public Color colorNormal = Color.white;

	public Color colorEnemy = Color.red;

	public Color colorTeam = Color.blue;

	public Color markerColor = Color.blue;

	public Color colorZombie = Color.green;

	public Color colorHuman = Color.blue;

	public LayerMask mask;

	private float maxDistance = 80f;

	private float maxVisebleDistance = 80f;

	private float minScale = 0.02f;

	private float maxScale = 0.45f;

	private string nickname = string.Empty;

	private PunTeams.Team team;

	private Vector3 startPos;

	private Vector3 oldPos;

	private PlayerBehavior player;

	private float yOffset;

	private void Awake()
	{
		startPos = base.transform.localPosition;
		oldPos = startPos;
		base.gameObject.SetActive(false);
	}

	public void SetLable(PlayerBehavior _player, string nick)
	{
		base.gameObject.SetActive(true);
		player = _player;
		nickname = nick;
		team = player.localTeam;
		textRender.sharedMaterial = teamMaterial;
		textField.text = nick;
	}

	private void fixedUpdate()
	{
	}

	private void LateUpdate()
	{
		if (PlayerBehavior.MyPlayer == null || player == null)
		{
			return;
		}
		textRender.enabled = !PlayerBehavior.MyPlayer.isSniperModeEnabled();
		if (!healthBar.backGround.enabled)
		{
			healthBar.backGround.enabled = true;
		}
		if (!healthBar.helathBar.enabled)
		{
			healthBar.helathBar.enabled = true;
		}
		team = player.localTeam;
		if (GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			if (TDMController.myTeam == PunTeams.Team.blue && team == PunTeams.Team.red)
			{
				maxVisebleDistance = 8f;
			}
			else
			{
				maxVisebleDistance = maxDistance;
			}
		}
		else
		{
			maxVisebleDistance = maxDistance;
		}
		if (player != null && healthBar != null)
		{
			nickname = player.props.nickname;
			if (player.isDead)
			{
				pointer.enabled = false;
				textField.text = string.Empty;
				healthBar.gameObject.SetActive(false);
				leaderCrown.enabled = false;
				return;
			}
			if (player.inCar || player.inHelic)
			{
				if (player.currentCar != null)
				{
					int num = ((player.currentCar.maxHealth == 0) ? 1 : player.currentCar.maxHealth);
					float num2 = (float)player.currentCar.health / (float)num;
					healthBar.percent = (float)player.currentCar.health / (float)player.currentCar.maxHealth;
				}
			}
			else
			{
				healthBar.percent = Mathf.Clamp01((float)(player.health + player.armor) / Mathf.Clamp(player.maxHealth + player.maxArmor, 1f, 10000f));
			}
		}
		Quaternion rotation = Quaternion.LookRotation(TPSCamera.instance.mainCamera.transform.position - base.transform.position);
		float num3 = Vector3.Distance(TPSCamera.instance.mainCamera.transform.position, base.transform.position);
		if (PlayerBehavior.MyPlayer.isSniperModeEnabled())
		{
			num3 /= 2f;
		}
		Vector3 vector = Vector3.one * Mathf.Clamp(num3 / maxDistance * maxScale / 2f, minScale, maxScale);
		base.transform.localScale = new Vector3(vector.x, vector.y, 0.02f);
		if (!player.inCar)
		{
			base.transform.localPosition = startPos + Vector3.up * Mathf.Clamp(num3 / maxDistance, 0f, 1f) * 3f;
		}
		if (player.inCar && player.currentCar != null)
		{
			base.transform.position = player.currentCar.carIndicatorHolder.position + Vector3.up * Mathf.Clamp(num3 / maxDistance, 1f, 2f);
		}
		Mortal mortal = null;
		if (GameController.instance.curTypeGame == TypeGame.TitanfallV2 && PlayerBehavior.MyPlayer.currentCar != null && PlayerBehavior.MyPlayer.currentCar is TitanBehavior)
		{
			mortal = (PlayerBehavior.MyPlayer.currentCar as TitanBehavior).TargetDetector.nearestEnemy;
		}
		else if (PlayerBehavior.MyPlayer != null && PlayerBehavior.MyPlayer.enemyWatcher != null && PlayerBehavior.MyPlayer.enemyWatcher.mortalLookTarget != null)
		{
			mortal = PlayerBehavior.MyPlayer.enemyWatcher.mortalLookTarget;
		}
		else if (GameController.instance.curTypeGame == TypeGame.WalkingRobots && PlayerBehavior.MyPlayer.enemyWatcher != null && PlayerBehavior.MyPlayer.enemyWatcher.mortalLookTarget != null)
		{
			mortal = PlayerBehavior.MyPlayer.enemyWatcher.mortalLookTarget;
		}
		if (player.imLeader || mortal == player || (mortal is EntityBehavior && ((EntityBehavior)mortal).getMainHuman() == player))
		{
			textField.text = nickname;
			healthBar.gameObject.SetActive(team == PunTeams.Team.none || team != TDMController.myTeam);
			pointer.enabled = false;
		}
		else if (num3 >= maxVisebleDistance || (!player.visible && !player.inCar && !player.inHelic))
		{
			if (team == TDMController.myTeam && team != 0 && num3 <= maxVisebleDistance * 2f)
			{
				pointer.enabled = player.visible;
				textField.text = string.Empty;
			}
			else
			{
				pointer.enabled = false;
				textField.text = string.Empty;
			}
			healthBar.gameObject.SetActive(false);
		}
		else
		{
			if (team != TDMController.myTeam)
			{
				if (!Physics.Linecast(base.transform.position, TPSCamera.instance.mainCamera.transform.position, mask))
				{
					textField.text = nickname;
					healthBar.gameObject.SetActive(GameController.instance.curTypeGame != TypeGame.race && GameController.instance.curTypeGame != TypeGame.RocketLeague);
				}
				else
				{
					textField.text = string.Empty;
					healthBar.gameObject.SetActive(false);
				}
			}
			else if (team == PunTeams.Team.none)
			{
				textField.text = string.Empty;
				healthBar.gameObject.SetActive(false);
			}
			else
			{
				textField.text = nickname;
				healthBar.gameObject.SetActive(false);
			}
			pointer.enabled = false;
		}
		if (player.imLeader)
		{
			pointer.material.color = colorLeader;
			textField.color = colorLeader;
			leaderCrown.enabled = textField.text != string.Empty && !player.isDead;
		}
		else
		{
			leaderCrown.enabled = false;
			if (GameController.instance.curTypeGame == TypeGame.Zombe)
			{
				if (team == PunTeams.Team.red)
				{
					pointer.material.color = colorZombie;
					textField.color = colorZombie;
				}
				else
				{
					pointer.material.color = colorHuman;
					textField.color = colorHuman;
				}
			}
			else if (team == PunTeams.Team.none)
			{
				textField.color = Color.white;
				pointer.material.color = Color.white;
			}
			else if (team == TDMController.myTeam)
			{
				pointer.material.color = colorTeam;
				textField.color = colorTeam;
			}
			else
			{
				textField.color = colorEnemy;
			}
		}
		base.transform.rotation = rotation;
	}
}
